﻿using System;
using System.Collections.Generic;
using System.Linq;
using NUnit.Framework;
using MVCQuick.Framework.Repository.NHibernate;
using MVCQuick.Framework.Repository;
using System.Diagnostics;
using MVCQuick.Framework.Logging;

namespace MVCQuick.Framework.Tests
{
    [TestFixture]
    public class RepositoryTests
    {
        public RepositoryTests()
        {
            Logger.ConfigureLog4Net(System.AppDomain.CurrentDomain.BaseDirectory,
                LogLevel.Debug, LogLevel.Debug);
        }

        [Test]
        public void MVCQuickCRUD()
        {
            using (SQLiteDbManager dbManager = new SQLiteDbManager())
            {
                dbManager.AddMapping("MVCQuickTest", typeof(RepositoryTests).Assembly);
                dbManager.UpdateSchema();

                IRepository repository = new NHibernateRepository(dbManager.OpenSession());

                User user = new User { Username = "admin", Email = "admin@163.com", Password = "admin123" };
                repository.Save(user);

                user = repository.Get<User>(1);
                user.Password = "123456";
                repository.Update(user);

                user = repository.Get<User>(1);
                repository.Delete(user);
            }
        }

        [Test]
        public void MVCQuickUnitOfWork()
        {
            using (SQLiteDbManager dbManager = new SQLiteDbManager())
            {
                dbManager.AddMapping("MVCQuickTest", typeof(RepositoryTests).Assembly);
                dbManager.UpdateSchema();

                IRepository repository = new NHibernateRepository(dbManager.OpenSession());

                var Users = new List<User>
                {
                    new User { Username = "刘备" , Email = "lb@123.com", Password = "123456" },
                    new User { Username = "曹操" , Email = "cc@123.com", Password = "123456"  },
                    new User { Username = "孙权" , Email = "sq@123.com", Password = "123456"  },
                    new User { Username = "曹植" , Email = "cz@123.com"},
                    new User { Username = "曹丕" , Email = "cp@123.com"},
                    new User { Username = "张飞" },
                    new User { Username = "关羽" },
                    new User { Username = "赵云" }
                };

                ITransaction unitOfWork = repository.BeginTransaction();

                foreach (var u in Users)
                {
                    repository.Save(u);
                }             
        
                unitOfWork.Commit();
            }
        }

        [Test]
        public void MVCQuickQuery()
        {
            using (SQLiteDbManager dbManager = new SQLiteDbManager())
            {
                dbManager.AddMapping("MVCQuickTest", typeof(RepositoryTests).Assembly);
                dbManager.UpdateSchema();

                IRepository repository = new NHibernateRepository(dbManager.OpenSession());

                var Users = new List<User>
                {
                    new User { Username = "刘备" , Email = "lb@123.com", Password = "123456" },
                    new User { Username = "曹操" , Email = "cc@123.com", Password = "123456"  },
                    new User { Username = "孙权" , Email = "sq@123.com", Password = "123456"  },
                    new User { Username = "曹植" , Email = "cz@123.com"},
                    new User { Username = "曹丕" , Email = "cp@123.com"},
                    new User { Username = "张飞" },
                    new User { Username = "关羽" },
                    new User { Username = "赵云" }
                };

                var Roles = new List<Role>
                {
                    new Role { Name = "皇帝"}
                };


                Debug.WriteLine("UnitOfWork Begin");
                ITransaction unitOfWork = repository.BeginTransaction();

                foreach (var u in Users)
                {
                    repository.Save(u);
                }

                foreach (var r in Roles)
                {
                    repository.Save(r);
                }

                foreach (var u in Users)
                {
                    if (new String[] { "刘备", "曹操", "孙权" }.Contains(u.Username))
                    {
                        ((IList<Role>)u.Roles).Add(
                            repository.Query<Role>().SingleOrDefault(x => x.Name == "皇帝"));

                        repository.Update(u);
                    }
                }

                IList<User> users;
                User user = null;

                users =
                    repository.Query<User>()
                    .ToList<User>();
                Assert.AreEqual(users.Count, 8);

                //Where
                users = repository.Query<User>()
                    .Where(x => x.Username == "刘备").ToList<User>();
                Assert.AreEqual(users.Count, 1);
                Assert.AreEqual(users[0].Username, "刘备");

                //OrderBy
                users = repository.Query<User>()
                  .OrderByDescending(x => x.Id).ToList();
                Assert.AreEqual(users[0].Username, "赵云");

                //Skip, Take
                users = repository.Query<User>()
                    .Skip(1 * 3).Take(3).ToList();
                Assert.AreEqual(users.Count, 3);
                Assert.AreEqual(users[0].Username, "曹植");



                //SingleOrDefault
                user = repository.Query<User>()
                    .SingleOrDefault(x => x.Username == "刘备");
                Assert.IsNotNull(user);
                Assert.AreEqual(user.Username, "刘备");
                user = repository.Query<User>()
                    .SingleOrDefault(x => x.Username == "诸葛亮");
                Assert.IsNull(user);


                user = repository.Query<User>()
                   .Where(x => x.Username == "刘备")
                   .Where(x => x.Email == "lb@123.com")
                  .SingleOrDefault();
                Assert.IsNotNull(user);
                Assert.AreEqual(user.Username, "刘备");


                //Count
                int count = repository.Query<User>()
                   .Where(x => x.Email != null || x.Email.Length <= 0)
                   .Count();
                Assert.AreEqual(count, 5);

                
              

            }
        }

        [Test]
        public void Linq()
        {

        }


        [Test]
        public void NHibernateLinq()
        {

        }
    }
}
